#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<string>
class Request
{
public:
    Request(int start, int end, const std::string &threadname)
    :_start(start), _end(end), _threadname(threadname)
    {}
public:
    int _start;
    int _end;
    std::string _threadname;
};
class Response
{
public:
    Response(int result, int exitcode)
    :_result(result), _exitcode(exitcode)
    {}
public:
    int _result; //计算结果
    int _exitcode; //计算结果是否可靠
};
void* sumCount(void* args)
{
    Request* rq = static_cast<Request*>(args);
    Response* rsp = new Response(0, 0);
    for(int i = rq->_start; i <= rq->_end; i++)
    {
        std::cout << rq->_threadname << "is runing, caling..." << i << std::endl;
        rsp->_result += i;
        usleep(100000);
    }
    delete rq;
    return rsp;
}
int main()
{
    pthread_t tid;
    Request* rq = new Request(1, 100, "thread 1");
    pthread_create(&tid, nullptr, sumCount, rq);
    void* ret;
    pthread_join(tid, &ret);
    Response* rsp = static_cast<Response*>(ret);
    std::cout << "rsp->result: " << rsp->_result << ",exitcode" <<rsp->_exitcode << std::endl;
    delete rsp;
    return 0;
}


// //可以被多个执行流同时执行
// void show(const std::string &name)
// {
//     std::cout << name << "say# " << "hello thread" << std::endl;
// }
// void* threadRoutine(void* args)
// {
//     const char* name = (const char*)args;
//     int cnt = 5;
//     while(cnt--)
//     {
//         std::cout << name << "pid " << getpid() << std::endl;
//         //show("[new thread]");
//         sleep(1);
//     }
//     pthread_exit((void*)100);
//     //exit(11);//终止进程的不能终止线程
//     //return (void*)1;
// }
// int main()
// {
//     pthread_t tid;
//     pthread_create(&tid, nullptr, threadRoutine, (void*)"Thread 1");
    
//     //sleep(1);
//     //pthread_cancel(tid);//取消目标线程
//     // while(true)
//     // {
//     //     std::cout << "main thread, pid " << getpid() << std::endl;
//     //     //show("[main thread]");
//     //     sleep(1);
//     // }
//     void* retval;
//     pthread_join(tid, &retval);
//     std::cout << "main thread quit ..., ret" << (long long int)retval << std::endl;
//     return 0;
// }